package com.molyfun.parents.activity.mainpage.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.im.ConsultActivity;
import com.molyfun.parents.rest.bean.CustomPlanlistResponse;
import com.molyfun.parents.utils.TimeUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:10/18/16.
 */
public class TrainRecommendView extends LinearLayout implements View.OnClickListener {

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.to_user_name)
    TextView mToUserName;
    @InjectView(R.id.chat_state)
    ImageView mChatState;
    @InjectView(R.id.rootlayout)
    LinearLayout mRootLayout;

    //评价布局
//    @InjectView(R.id.satisfaction_degree_layout)
//    LinearLayout mSatisfactionDegreelayout;
    @InjectView(R.id.time_start)
    TextView mTimeStart;
    @InjectView(R.id.end_start)
    TextView mTimeEnd;
    @InjectView(R.id.satisfaction_degree_tv)
    TextView mSatisfactionDegreeTv;
    @InjectView(R.id.end_time_layout)
    LinearLayout mEndTimeLayout;
//    @InjectView(R.id.msg_count)
//    TextView mMsgCount;
    @InjectView(R.id.line)
    View mLine;

    private static final int REFRESH_COUNT = 1001;

    private CustomPlanlistResponse.PlanListBean mPlanListBean;

//    private EMConversation conversation;

    public TrainRecommendView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public TrainRecommendView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public TrainRecommendView(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_train_recommend, this);
        ButterKnife.inject(this);
        setOnClickListener(this);
        mRootLayout.setOnClickListener(this);
    }

    public void initData(CustomPlanlistResponse.PlanListBean planListBean) {
        initData(planListBean, true);
    }

    /**
     * init view
     */
    public void initData(CustomPlanlistResponse.PlanListBean planListBean, boolean showLine) {
        if (planListBean == null) {
            return;
        }
        mPlanListBean = planListBean;
        mToUserName.setText(planListBean.getUsername());
        if (TextUtils.isEmpty(planListBean.getStatus()) || planListBean.getStatus().equals("0")) {
            mChatState.setImageResource(R.mipmap.ic_consultation_doing);
            mTimeEnd.setText("待定");
//            EMClient.getInstance().chatManager().addMessageListener(this);

            mTimeStart.setText(TimeUtils.transForDateTime(planListBean.getBegindatetime()));

//            conversation = EMClient.getInstance().chatManager().getConversation(planListBean.getImgroupid(), EMConversation.EMConversationType.GroupChat, true);
//
//            long msgCount = mPlanListBean.getUnreadMessageNum();
//            if (msgCount > 0) {
//                mMsgCount.setVisibility(View.VISIBLE);
//                if (msgCount >= 100) {
//                    mMsgCount.setText("99+");
//                } else {
//                    mMsgCount.setText(msgCount + "");
//                }
//
//            } else {
//                mMsgCount.setVisibility(View.GONE);
//            }

            mSatisfactionDegreeTv.setText("暂无");
        } else {
            mChatState.setImageResource(R.mipmap.ic_consultation_done);
//            mMsgCount.setVisibility(View.GONE);
            mTimeEnd.setText(TimeUtils.transForDateTime(planListBean.getEnddatetime()));

            mTimeStart.setText(TimeUtils.transForDateTime(planListBean.getBegindatetime()));
            if (TextUtils.isEmpty(planListBean.getDegree()) || planListBean.getDegree().equals("-1")) {
                //没有评价
            } else {
                //满意度 1:满意 2:一般 3:不满意
                if (planListBean.getDegree().equals("1")) {
                    mSatisfactionDegreeTv.setText("满意");
                } else if (planListBean.getDegree().equals("2")) {
                    mSatisfactionDegreeTv.setText("一般");
                } else if (planListBean.getDegree().equals("3")) {
                    mSatisfactionDegreeTv.setText("不满意");
                }else{
                    mSatisfactionDegreeTv.setText("暂无");
                }
            }

        }
        if(showLine){
            mLine.setVisibility(View.VISIBLE);
        }else{
            mLine.setVisibility(View.GONE);
        }

    }

    @Override
    public void onClick(View v) {
        //TODO 跳转到聊天详情页
        ConsultActivity.startActivity(getContext(), mPlanListBean, mPlanListBean.getImgroupid());
    }

//    @Override
//    public void onCmdMessageReceived(List<EMMessage> list) {
//    }
//
//    @Override
//    public void onMessageChanged(EMMessage emMessage, Object o) {
//    }
//
//    @Override
//    public void onMessageDelivered(List<EMMessage> list) {
//    }
//
//    @Override
//    public void onMessageRead(List<EMMessage> list) {
//    }
//
//    @Override
//    public void onMessageRecalled(List<EMMessage> list) {
//    }
//
//    @Override
//    public void onMessageReceived(List<EMMessage> list) {
//        handler.sendEmptyMessage(REFRESH_COUNT);
//    }
//
//    Handler handler = new Handler() {
//        @Override
//        public void handleMessage(android.os.Message message) {
//            switch (message.what) {
//                case REFRESH_COUNT:
//                default:
//                    if (conversation != null && mMsgCount != null) {
//
//                        int msgCount = conversation.getUnreadMsgCount();
//                        System.out.println("onMessageReceived--->" + msgCount);
//                        if (msgCount > 0) {
//                            mMsgCount.setVisibility(View.VISIBLE);
//                            if (msgCount >= 100) {
//                                mMsgCount.setText("99+");
//                            } else {
//                                mMsgCount.setText(msgCount + "");
//                            }
//
//                        } else {
//                            mMsgCount.setVisibility(View.GONE);
//                        }
//                    }
//                    break;
//            }
//        }
//    };

}